package com.wave.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wave.common.annotation.Log;
import com.wave.common.annotation.RepeatSubmit;
import com.wave.common.core.controller.BaseController;
import com.wave.common.core.domain.PageQuery;
import com.wave.common.core.domain.R;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.core.validate.AddGroup;
import com.wave.common.core.validate.EditGroup;
import com.wave.common.enums.BusinessType;
import com.wave.common.utils.poi.ExcelUtil;
import com.wave.system.domain.bo.SysProduceRecordBo;
import com.wave.system.domain.vo.SysProduceRecordVo;
import com.wave.system.domain.vo.SysProductivityMonitorVo;
import com.wave.system.service.ISysProduceRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 生产记录
 *
 * @author wave
 * @date 2024-10-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/produceRecord")
public class SysProduceRecordController extends BaseController {

    private final ISysProduceRecordService iSysProduceRecordService;

    /**
     * 查询生产记录列表
     */
    @SaCheckPermission("system:produceRecord:list")
    @GetMapping("/list")
    public TableDataInfo<SysProduceRecordVo> list(SysProduceRecordBo bo, PageQuery pageQuery) {
        return iSysProduceRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出生产记录列表
     */
    @SaCheckPermission("system:produceRecord:export")
    @Log(title = "生产记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysProduceRecordBo bo, HttpServletResponse response) {
        List<SysProduceRecordVo> list = iSysProduceRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "生产记录", SysProduceRecordVo.class, response);
    }

    /**
     * 获取生产记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:produceRecord:query")
    @GetMapping("/{id}")
    public R<SysProduceRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iSysProduceRecordService.queryById(id));
    }

    /**
     * 新增生产记录
     */
    @SaCheckPermission("system:produceRecord:add")
    @Log(title = "生产记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysProduceRecordBo bo) {
        return toAjax(iSysProduceRecordService.insertByBo(bo));
    }

    /**
     * 修改生产记录
     */
    @SaCheckPermission("system:produceRecord:edit")
    @Log(title = "生产记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysProduceRecordBo bo) {
        return toAjax(iSysProduceRecordService.updateByBo(bo));
    }

    /**
     * 删除生产记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:produceRecord:remove")
    @Log(title = "生产记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysProduceRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 产能监控
     * @return
     */
    @GetMapping("/monitor")
    public R<SysProductivityMonitorVo> getMonitorInfo(@RequestParam(value = "dateStr") String dateStr) {
        return R.ok(iSysProduceRecordService.getMonitorInfo(dateStr));
    }
}
